<?php
/**
 * Created by PhpStorm.
 * User: cth
 * Date: 2017/6/13
 * Time: 上午10:13
 */

namespace App\Repositories;

use App\Models\Order;
use App\Models\OrderQueue;
use App\Models\Shop;
use App\Models\ShopRoomTable;
use App\Models\WeixinUser;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrderQueueRepository
{
    //创建排队订单
    public function createQueueOrder($request)
    {
        //小程序
        if($request->token){
            $WeixinUser = WeixinUser::where('token',$request->token)->first();
            if($WeixinUser){
                $user_id = $WeixinUser['user_id'];
            }else{
                return show(209,'用户不存在');
            }
            //web端
        }else {
            $user_id = Auth::id();
            if (!$user_id) {
                return show(203, '未登录');
            }
        }
        $shop=Shop::find($request->shop_id);
        if(!$shop){
            return show("206","商铺不存在");
        }
        if($shop->is_check!=1){
            return show("207","商铺未审核");
        }
        if($shop->is_list!=1){
            return show("208","该商铺未开通排队功能");
        }
        $people = $request['people'];
        //根据人数匹配房桌
        if($people<=4) {
            $size = 1;//小
        }
        if($people<=6 && $people>4) {
            $size = 2;//中
        }
        if($people>6) {
            $size = 3;//大
        }
        //房桌不限
        if($request['type']==2){
            $roomAndtable = ShopRoomTable::where('shop_id',$request['shop_id'])
                ->whereBetween('hold',[$people,$people+4])
                ->where('is_use',0)
                ->first();
            //有空位
            if($roomAndtable) {
                return show('205','无需排队');
            }
        }else {//房子或桌子
            $roomAndtable = ShopRoomTable::where('shop_id', $request['shop_id'])
                ->whereBetween('hold',[$people,$people+4])
                ->where('is_use', 0)
                ->where('type', $request['type'])
                ->first();
            //有空位
            if ($roomAndtable) {
                return show('205', '无需排队');
            }
        }
        //没空位，排队
        if($request['type']==1){
            $type = 'A';//桌子号牌
        }else if($request['type']==2){
            $type = 'B';//房间号牌
        }else{
            $type = 'C';//不限
        }
        //生成号牌
        $data = OrderQueue::where('number', 'like', $type.'%')
            ->where('size',$size)
            ->where('shop_id',$request['shop_id'])
            ->orderBy('id','desc')
            ->first();
        if($data){
            $number = substr($data['number'],1)+1;
        }else if($size == 1){
            $number = 1001;
        }else if($size == 2){
            $number = 2001;
        }else if($size == 3){
            $number = 3001;
        }
        //生成号牌
        $number = $type.$number;
        //用餐时间
        $today = date('Y-m-d',time());
        $post_day = date('Y-m-d',$request['eat_time']);
        if($today!=$post_day){
            return show(202,'时间传参错误');
        }
        $date = date('Y-m-d H:i:s',$request['eat_time']);
        //创建排队订单
        DB::beginTransaction();
        $orderId = DB::table('orders')->insertGetId([
            'order_number' => randID(),
            'order_type' => '2',
            'user_id' => $user_id,
            'order_price' => '2',
            'order_amount' => '2',
            'order_status' => '1',
            'shop_id' => $request['shop_id'],
            'created_at' => date('y-m-d H:i:s',time())
        ]);
        $queue = DB::table('order_queue')->insert([
            'user_id' => $user_id,
            'size' => $size,
            'shop_id' => $request['shop_id'],
            'order_id' => $orderId,
            'eat_time' => $date,
            'status' => 0,
            'people' => $request['people'],
            'number' => $number,
            'created_at' => date('y-m-d H:i:s',time())
        ]);
        if($orderId && $queue){
            DB::commit();
            $res['id'] = $orderId;
            $res['order_price'] = 2;
            $res['order_amount'] = 2;
            $res['order_type'] = 2;
            return show(200,'排队订单创建成功',$res);
        }else{
            DB::rollBack();
            return show(201,'排队订单创建失败');
        }
    }

    //我的排队订单
    public function showQueueOrder()
    {
        $user_id = Auth::id();
        //查询排队订单数据
        $my_queque = OrderQueue::getMyQueue($user_id);
        if(empty($my_queque)) {
            return show(201, 'empty');
        }
        foreach ($my_queque as $key => $value) {
            //所选位置
            $type = substr($value['number'], 0, 1);
            //规定用餐时间段
            $before_time = date('Y-m-d H:i:s',strtotime('-1 minute',strtotime($value['eat_time'])));
            $after_time = date('Y-m-d H:i:s',strtotime('+1 hour -1 minute',strtotime($value['eat_time'])));
            //选择位置不限时
            if($type == 'C'){
                //桌子等待人数
                $desk_waiting = OrderQueue::getWaitingPeople('A',$value,$before_time,$after_time);
                //房间等待人数
                $room_waiting = OrderQueue::getWaitingPeople('B',$value,$before_time,$after_time);
                if($desk_waiting>$room_waiting){
                    $wait_people=$room_waiting;
                }else{
                    $wait_people=$desk_waiting;
                }
                //选择房间或桌子时
            }else {
                //等待人数
                $wait_people = OrderQueue::getWaitingPeople($type,$value,$before_time,$after_time);
            }
            $value['waiting'] = $wait_people;
        }
        return show(200, 'ok', $my_queque);
    }

    //排队信息
    public function showQueueMessage($request)
    {
        $user_id = Auth::id();
        $data = OrderQueue::where('status',0)
            ->where('user_id','<>',$user_id)
            ->where('shop_id',$request['shop_id'])
            ->get();
        return show(200, 'ok', $data);
    }
}
